package com.haixiaoke.saas.order.state.role;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.haixiaoke.saas.common.constant.LockConstants;
import com.haixiaoke.saas.common.enums.OrderStateEnum;
import com.haixiaoke.saas.common.enums.OrderTabEnum;
import com.haixiaoke.saas.lock.domain.HouseLock;
import com.haixiaoke.saas.lock.domain.LockFace;
import com.haixiaoke.saas.lock.domain.LockFreezeLog;
import com.haixiaoke.saas.order.domain.Order;
import com.haixiaoke.saas.order.state.server.IOrderStateServerRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 退租方法实现
 *
 * @author qixi
 */
public class RefundRentOrderOrderStateServerRole extends AbstractSpecificOrderOrderStateServerRole {
    private static final Logger LOG = LoggerFactory.getLogger(RefundRentOrderOrderStateServerRole.class);

    public RefundRentOrderOrderStateServerRole(IOrderStateServerRole stateServerRole) {
        super(stateServerRole);
    }

    @Override
    @Transactional
    public void refundRent(Order order) {
        String orderId = order.getOrderId();
        houseMemberService.rentingTerminationByOrderId(orderId);
        orderLesseeService.rentingTerminationByOrderId(orderId);

        addOrderStateRecord(order.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_REFUND_RENT.getValue());
        order.setOrderState(OrderStateEnum.ORDER_STATE_REFUND_RENT.getValue());
        order.setCheckOutApplicationTime(new Date());


        // 添加房管家人脸信息
        addHouseManager(order);

        orderService.updateOrder(order);
    }

    private void addHouseManager(Order order) {
        List<LockFace> lockFaceList = lockFaceService.selectLockFaceListAndHouseIdAndUserTypeAndFaceState(order.getHouseId(), LockConstants.USER_TYPE_MANAGER, LockConstants.FACE_STATE_DISABLE);
        if (CollectionUtil.isNotEmpty(lockFaceList)) {
            for (LockFace lockFace : lockFaceList) {
                houseLockService.authorizationLockFace(lockFace.getMac(), lockFace);
            }
        }

        List<HouseLock> houseLockList = houseLockService.selectHouseLockByHouseIdAndLockState(order.getHouseId(), LockConstants.LOCK_STATE_FREEZE);
        if (CollectionUtil.isNotEmpty(houseLockList)) {
            houseLockService.updateHouseLockState(order.getHouseId(), houseLockList);
        }
    }


    @Override
    @Transactional
    public void approved(Order order) {
        this.refundRent(order);
    }

    @Override
    public void disagree(Order order) {
        String orderId = order.getOrderId();
        // 原始订单
        Order originalOrder = orderService.selectOrderByOrderId(orderId);
        originalOrder.setOrderState(OrderStateEnum.ORDER_STATE_SIGNED.getValue());

        addOrderStateRecord(originalOrder.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_SIGNED.getValue());
        orderService.updateOrder(originalOrder);
    }

    /**
     * 验房
     */
    @Override
    public void houseInspection(Order order) {
        Date checkOutTime = order.getCheckOutTime();
        Date realityCheckOutTime = order.getRealityCheckOutTime();

        // 最新订单结束日期等于签约结束日期
        if (DateUtil.isSameDay(checkOutTime, realityCheckOutTime)) {
            // 更新订单状态
            addOrderStateRecord(order.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_EXPIRE_REFUND_RENT.getValue());
            order.setOrderState(OrderStateEnum.ORDER_STATE_EXPIRE_REFUND_RENT.getValue());
            order.setOrderTab(OrderTabEnum.ORDER_TAB_OFF_LEASE.getValue());
            orderService.updateOrder(order);
        } else if (realityCheckOutTime.getTime() < checkOutTime.getTime()) {

            addOrderStateRecord(order.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_NEGOTIATE_REFUND_RENT.getValue());
            order.setOrderState(OrderStateEnum.ORDER_STATE_NEGOTIATE_REFUND_RENT.getValue());
            order.setOrderTab(OrderTabEnum.ORDER_TAB_OFF_LEASE.getValue());
            orderService.updateOrder(order);
        }


    }


}
